package com.rxt.common.exception;

import com.rxt.common.result.Result;
import com.rxt.common.result.ResultCodeEnum;
import io.jsonwebtoken.ExpiredJwtException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常处理
 */
@Slf4j
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {

    @ExceptionHandler(CustomException.class)
    public Result exceptionHandler(CustomException e) {
        log.warn("CustomException: code-{}, msg-{}", e.getCode(), e.getMessage());
//        e.printStackTrace();
        return Result.fail(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(ExpiredJwtException.class)
    public Result jwtException(ExpiredJwtException e) {
        e.printStackTrace();
        return Result.fail(4001, "token已过期!");

    }

    @ExceptionHandler(Exception.class)
    public Result exceptionHandler(Exception e) {
        e.printStackTrace();
        return Result.fail(ResultCodeEnum.SERVER_BUSY);
    }
}
